<!DOCTYPE html>
<!--suppress ALL-->
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>商品管理</title>
    <link rel="stylesheet" type="text/css" href="/static/jquery-easyui-1.7.0/themes/metro-blue/easyui.css"/>
    <link rel="stylesheet" type="text/css" href="/static/jquery-easyui-1.7.0/themes/icon.css"/>
    <script type="text/javascript" src="/static/jquery-easyui-1.7.0/jquery.min.js"></script>
    <script type="text/javascript" src="/static/jquery-easyui-1.7.0/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="/static/jquery-easyui-1.7.0/locale/easyui-lang-zh_CN.js"></script>
    <script type="text/javascript" src="/static/ckeditor/ckeditor.js"></script>
    <script type="text/javascript">

        var url;

        function searchGoods() {
            $("#dg").datagrid('load', {
                "name": $("#s_name").val(),
                "userName": $("#s_userName").val(),
                "state": $("#s_state").val(),
                "isRecommend": $("#s_isRecommend").val(),
                "goodsTypeId": $("#s_goodsTypeId").val()
            });
        }

        function resetValue() {
            $("#s_name").val("");
            $("#s_userName").val("");
            $("#s_state").combobox("setValue", "");
            $("#s_isRecommend").combobox("setValue", "");
            $("#s_goodsTypeId").combobox("setValue", "");
            $("#recommendDays").numberbox('setValue', "")
            $("#reason").val("");
        }

        function deleteGoods() {
            var selectedRows = $("#dg").datagrid("getSelections");
            if (selectedRows.length != 1) {
                $.messager.alert("系统提示", "请选择一个要删除的商品!");
                return;
            }
            var strIds = [];
            for (var i = 0; i < selectedRows.length; i++) {
                strIds.push(selectedRows[i].id);
            }
            var ids = strIds.join(",");
            $.messager.confirm("系统提示", "你确定要删除这<span style='color: red'>" + selectedRows.length + "</span>条数据吗?", function (r) {
                if (r) {
                    $.post("/admin/goods/delete", {ids: ids}, function (result) {
                        if (result.success) {
                            $.messager.alert("系统提示", "数据已成功删除！");
                            $("#dg").datagrid("reload");
                        } else {
                            $.messager.alert("系统提示", "数据删除失败！");
                        }
                    }, "json");
                }
            });
        }

        function recommendGoodsOrNot() {
            var selectedRows = $("#dg").datagrid("getSelections");
            if (selectedRows.length != 1) {
                $.messager.alert("系统提示", "请选择一个商品!");
                return;
            }
            var id = selectedRows[0].id;
            var isRecommend = selectedRows[0].isRecommend;
            var goodsTypeName = selectedRows[0].goodsTypeName;
            if (goodsTypeName === "求购") {
                $.messager.alert("系统提示", "求购类无法被推荐哦!!");
                return;
            }
            var state = selectedRows[0].state;
            if (state !== 1) {
                $.messager.alert("系统提示", "只有<span style='color: red'>上架中</span>的商品才能被推荐哦!!");
                return;
            }
            if (isRecommend == 0) {
                $.messager.confirm("系统提示", "你确定要<span style='color: red'>推荐</span>这个商品吗?", function (r) {
                    if (r) {
                        $("#dlg2").dialog("open").dialog("setTitle", "输入推荐天数");
                        url = "/admin/goods/recommendGoodsOrNot?id=" + id + "&isRecommend=1&recommendDays=" + $("#recommendDays").numberbox('getValue');
                    }
                });
            } else {
                $.messager.confirm("系统提示", "你确定要<span style='color: red'>不推荐</span>这个商品吗?", function (r) {
                    if (r) {
                        $.post("/admin/goods/recommendGoodsOrNot", {id: id, isRecommend: 0}, function (result) {
                            if (result.success) {
                                $.messager.alert("系统提示", "不推荐成功！");
                                $("#dg").datagrid("reload");
                            } else {
                                $.messager.alert("系统提示", "不推荐失败！");
                            }
                        }, "json");
                    }
                });
            }
        }

        function auditGoodsSuccess() {
            var selectedRows = $("#dg").datagrid("getSelections");
            if (selectedRows.length != 1) {
                $.messager.alert("系统提示", "请选择一个商品!");
                return;
            }
            var id = selectedRows[0].id;
            if (selectedRows[0].state != 0) {
                $.messager.alert("系统提示", "只能处理未审核的商品哦！");
                return;
            }
            $.messager.confirm("系统提示", "你确定要<span style='color: red'>审核通过</span>这个商品吗?", function (r) {
                if (r) {
                    $.post("/goods/updateGoodsState", {goodsId: id, state: 1}, function (result) {
                        if (result.success) {
                            $.messager.alert("系统提示", "审核通过成功！");
                            $("#dg").datagrid("reload");
                        } else {
                            $.messager.alert("系统提示", "审核通过失败！");
                        }
                    }, "json");
                }
            });
        }

        function auditGoodsFail() {
            var selectedRows = $("#dg").datagrid("getSelections");
            if (selectedRows.length != 1) {
                $.messager.alert("系统提示", "请选择一个商品!");
                return;
            }
            var id = selectedRows[0].id;
            var state = selectedRows[0].state;
            var reason = $("#reason").val();
            if (state === 2) {
                $.messager.alert("系统提示", "该商品的状态已经是<span style='color: red'>审核不通过</span>了!!");
                return;
            }
            $.messager.confirm("系统提示", "你确定要将商品状态设置为<span style='color: red'>审核不通过</span>吗?", function (r) {
                if (r) {
                    $("#dlg3").dialog("open").dialog("setTitle", "输入审核不通过的理由");
                    url = "/goods/updateGoodsState?goodsId=" + id + "&state=2&reason=" + reason;
                }
            });
        }

        function openGoodslAddDialog() {
            $("#dlg").dialog("open").dialog("setTitle", "添加商品信息");
            url = "/admin/goods/save";
        }

        function openGoodsModifyTab() {
            var selectedRows = $("#dg").datagrid("getSelections");
            if (selectedRows.length != 1) {
                $.messager.alert("系统提示", "请选择一个要查看的商品!");
                return;
            }
            var row = selectedRows[0];
            $("#dlg").dialog("open").dialog("setTitle", "查看商品");
            $("#fm").form("load", row);
            CKEDITOR.instances.content.setData(row.content);
            url = "/admin/goods/save?id=" + row.id;
            console.log(url);
        }

        function submitData() {
            $("#fm").form("submit", {
                url: url,
                onSubmit: function () {
                    console.log(url);
                    var content = CKEDITOR.instances.content.getData();
                    if (content == "") {
                        $.messager.alert("系统提示", "内容不能为空!");
                        return false;
                    }
                    return $(this).form("validate");
                },
                success: function (result) {
                    var result = eval('(' + result + ')');
                    if (result.success) {
                        $.messager.alert("系统提示", "保存成功!");
                        $("#dg").datagrid("reload");
                        $("#dlg").dialog("close");
                        resetValue();
                    } else {
                        $.messager.alert("系统提示", "保存失败！");
                    }
                }
            })
        }

        function submitData2() {
            $("#fm2").form("submit", {
                url: url,
                onSubmit: function () {
                    return $(this).form("validate");
                },
                success: function (result) {
                    var result = eval('(' + result + ')');
                    if (result.success) {
                        $.messager.alert("系统提示", "推荐商品成功!");
                        $("#dg").datagrid("reload");
                        $("#dlg2").dialog("close");
                        $("#recommendDays").numberbox('setValue', "")
                    } else {
                        $.messager.alert("系统提示", "推荐商品失败！");
                    }
                }
            })
        }

        function submitData3() {
            $("#fm3").form("submit", {
                url: url,
                onSubmit: function () {
                    return $(this).form("validate");
                },
                success: function (result) {
                    var result = eval('(' + result + ')');
                    if (result.success) {
                        $.messager.alert("系统提示", "将商品状态设置为审核不通过成功!");
                        $("#dg").datagrid("reload");
                        $("#dlg3").dialog("close");
                        $("#reason").val("");
                    } else {
                        $.messager.alert("系统提示", "将商品状态设置为审核不通过失败！");
                    }
                }
            })
        }

        function closeGoodsDialog() {
            $("#dlg").dialog("close");
            $("#dg").datagrid("reload");
            resetValue();
        }

        function closeDialog2() {
            $("#dlg2").dialog("close");
            $("#recommendDays").numberbox('setValue', "")
        }

        function closeDialog3() {
            $("#dlg3").dialog("close");
            $("#reason").val("");
        }

        $(document).ready(function () {

            $("#dg").datagrid({
                onDblClickRow: function (index, row) {
                    $("#dlg").dialog("open").dialog("setTitle", "查看商品信息");
                    $("#fm").form("load", row);
                    CKEDITOR.instances.content.setData(row.content);
                    url = "/admin/goods/save?id=" + row.id;
                }
            });
        });

        function formatType(val, row) {
            return val.name;
        }

        function formatName(val, row) {
            if (val.length > 20) {
                return "<span title='" + val + "'>" + val.substring(0, 20) + "...</span>";
            } else {
                return val;
            }
        }

        function formatPriceNow(val, rows) {
            return "<span style='color: #0c9076'>" + val + "</span>";
        }

        function formatState(val, row) {
            if (val == 0) {
                return "<span style='color: #0000FF'>未审核</span>";
            } else if (val == 1) {
                return "<span style='color: #0c9076'>上架中</span>";
            } else if (val == 2) {
                return "<span style='color: red' title='" + row.reason + "'>审核不通过</span>";
            } else if (val == 3) {
                return "已下架";
            } else if (val == 4) {
                return "被预订";
            } else if (val == 4) {
                return "交易成功";
            }
        }

        function formatIsRecommend(val, row) {
            if (val == 0) {
                return "不推荐";
            } else if (val == 1) {
                return "<span style='color: #0000FF'>被推荐</span>";
            }
        }
    </script>
</head>
<body style="margin: 1px">
<table id="dg" title="商品管理" class="easyui-datagrid" striped="true"
       fitColumns="true" pagination="true" rownumbers="true" singleSelect="true"
       url="/admin/goods/list" fit="true" toolbar="#tb">
    <thead>
    <tr>
        <th field="cb" checkbox="true" align="center"></th>
        <th field="id" width="20" align="center">编号</th>
        <th field="userName" width="20" align="center">用户名</th>
        <th field="name" width="100" align="center" formatter="formatName">名称</th>
        <th field="state" width="20" align="center" formatter="formatState">状态</th>
        <th field="isRecommend" width="20" align="center" formatter="formatIsRecommend">是否推荐</th>
        <th field="priceNow" width="20" align="center" formatter="formatPriceNow">现在价格</th>
        <th field="priceLast" width="20" align="center">上次价格</th>
        <th field="goodsTypeName" width="30" align="center">分类名称</th>
    </tr>
    </thead>
</table>
<div id="tb">
    <div>
        <a href="javascript:openGoodsModifyTab()" class="easyui-linkbutton" iconCls="icon-search" plain="true">查看</a>
        <a href="javascript:deleteGoods()" class="easyui-linkbutton" iconCls="icon-remove" plain="true">删除</a>
        <a href="javascript:recommendGoodsOrNot()" class="easyui-linkbutton" iconCls="icon-recommendGoods" plain="true">推荐与否</a>
        <a href="javascript:auditGoodsSuccess()" class="easyui-linkbutton" iconCls="icon-ok" plain="true">审核通过</a>
        <a href="javascript:auditGoodsFail()" class="easyui-linkbutton" iconCls="icon-no" plain="true">审核不通过</a>
    </div>
    <div>
        &nbsp;标题:&nbsp;<input type="text" id="s_name" size="20"
                              onkeydown="if(window.event.keyCode===13) searchGoods()"/>
        &nbsp;用户名:&nbsp;<input type="text" id="s_userName" size="20"
                               onkeydown="if(window.event.keyCode===13) searchGoods()"/>
        &nbsp;商品类型:&nbsp;<input class="easyui-combobox" id="s_goodsTypeId" style="width: 140px"
                                editable="true"
                                data-options="panelHeight:'auto',valueField:'id',textField:'name',url:'/admin/goodsType/comboList'"/>
        &nbsp;状态:&nbsp;
        <select id="s_state" class="easyui-combobox" style="width:115px;" data-options="panelHeight:'auto'">
            <option value="">请选择...</option>
            <option value="0">未审核</option>
            <option value="1">上架中</option>
            <option value="2">审核不通过</option>
            <option value="3">已下架</option>
            <option value="4">被预订</option>
            <option value="5">交易成功</option>
        </select>
        &nbsp;是否推荐:&nbsp;
        <select id="s_isRecommend" class="easyui-combobox" style="width:100px;" data-options="panelHeight:'auto'">
            <option value="">请选择...</option>
            <option value="0">不推荐</option>
            <option value="1">被推荐</option>
        </select>
        <a href="javascript:searchGoods()" class="easyui-linkbutton" iconCls="icon-search" plain="true">搜索</a>
        <a href="javascript:resetValue()" class="easyui-linkbutton" iconCls="icon-reset" plain="true">重置</a>
    </div>
</div>
<div id="dlg" class="easyui-dialog" style="width: 1200px;height: 580px;padding: 10px 20px"
     closed="true" buttons="#dlg-buttons" data-options="onClose:function(){resetValue()}">
    <form action="" id="fm" method="post" enctype="multipart/form-data">
        <table cellspacing="20px">
            <tr>
                <td width="80px">名称：</td>
                <td>
                    <input type="text" id="name" name="name" style="width: 400px" required="true"
                           class="easyui-validatebox"/>
                </td>
            </tr>
            <tr>
                <td width="80px">类型：</td>
                <td>
                    <input type="text" id="goodsTypeName" name="goodsTypeName" style="width: 400px" required="true"
                           class="easyui-validatebox"/>
                </td>
            </tr>
            <tr>
                <td width="80px">现在价格：</td>
                <td>
                    <input type="text" id="priceNow" name="priceNow" style="width: 400px" required="true"
                           class="easyui-validatebox"/>
                </td>
            </tr>
            <tr>
                <td width="80px">上次价格：</td>
                <td>
                    <input type="text" id="priceLast" name="priceLast" style="width: 400px" required="true"
                           class="easyui-validatebox"/>
                </td>
            </tr>
            <tr>
                <td valign="top">详情：</td>
                <td>
                    <textarea id="content" name="content" rows="30" cols="80"></textarea>
                </td>
            </tr>
        </table>
    </form>
</div>
<div id="dlg-buttons">
<!--    <a href="javascript:submitData()" class="easyui-linkbutton" iconCls="icon-ok">保存</a>-->
    <a href="javascript:closeGoodsDialog()" class="easyui-linkbutton" iconCls="icon-cancel">关闭</a>
</div>
<div id="dlg2" class="easyui-dialog" style="width: 400px;height: 200px;padding: 10px 20px"
     closed="true" buttons="#dlg-buttons2" data-options="onClose:function(){resetValue()}">
    <form action="" id="fm2" method="post" enctype="multipart/form-data">
        <table cellspacing="20px">
            <tr>
                <td width="80px">推荐天数：</td>
                <td>
                    <input type="text" id="recommendDays" name="recommendDays" style="width: 200px" required="true"
                           class="easyui-numberbox"/>
                </td>
            </tr>
        </table>
    </form>
</div>
<div id="dlg-buttons2">
    <a href="javascript:submitData2()" class="easyui-linkbutton" iconCls="icon-ok">保存</a>
    <a href="javascript:closeDialog2()" class="easyui-linkbutton" iconCls="icon-cancel">关闭</a>
</div>
<div id="dlg3" class="easyui-dialog" style="width: 640px;height: 320px;padding: 10px 20px"
     closed="true" buttons="#dlg-buttons3" data-options="onClose:function(){resetValue()}">
    <form action="" id="fm3" method="post" enctype="multipart/form-data">
        <table cellspacing="20px">
            <tr>
                <td valign="top">理由：</td>
                <td>
                    <textarea id="reason" name="reason" rows="10" cols="60" class="easyui-validatebox"
                              required="true"></textarea>
                </td>
            </tr>
        </table>
    </form>
</div>
<div id="dlg-buttons3">
    <a href="javascript:submitData3()" class="easyui-linkbutton" iconCls="icon-ok">保存</a>
    <a href="javascript:closeDialog3()" class="easyui-linkbutton" iconCls="icon-cancel">关闭</a>
</div>
<script type="text/javascript">
    CKEDITOR.replace("content");
</script>
</body>
</html>